package com.example.tvrecyclerview.fragment;

import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.LayoutRes;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.example.tvrecyclerview.R;

import java.util.List;

/**
 * @author: njb
 * @date: 2020/9/24 0024 1:03
 * @desc: fragment基类
 */
public abstract class FragmentBase extends Fragment{
    protected final String TAG = getClass().getSimpleName();
    private View rootView;
    private OnCurrentFragmentHandler currentFragmentHandler;

    protected final void showFragment(FragmentBase fragment, boolean addToBackStack) {
        showFragment(getFragmentManager(), fragment, addToBackStack);
    }

    protected final void showChildFragment(FragmentBase fragment, boolean addToBackStack) {
        showFragment(getChildFragmentManager(), fragment, addToBackStack);
    }


    protected void showFragment(FragmentManager fragmentManager, FragmentBase fragment, boolean addToBackStack) {
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.frame_content, fragment, fragment.getClass().getName());
        if (addToBackStack) {
            fragmentTransaction.addToBackStack(fragment.getClass().getName());
        }
        fragmentTransaction.commit();
    }

    protected final <T extends FragmentBase> FragmentBase findFragmentByTag(Class<T> fragmentClass) {
        String tag = fragmentClass.getName();
        return findFragmentByTag(tag);
    }

    protected final FragmentBase findFragmentByTag(String tag) {
        FragmentBase fragment = (FragmentBase) getFragmentManager().findFragmentByTag(tag);
        if (fragment == null) {
            fragment = (FragmentBase) getChildFragmentManager().findFragmentByTag(tag);
        }
        return fragment;
    }



    protected final <T extends FragmentBase> boolean isFragmentExist(Class<T> fragmentClass) {
        return findFragmentByTag(fragmentClass) != null;
    }

    protected final boolean isFragmentExist(String tag) {
        return findFragmentByTag(tag) != null;
    }


    @LayoutRes
    protected abstract int getLayoutRes();

    protected void initView(View rootView) {
        //Log.d(TAG, "initView: ");
    }



    /**
     * called when back to fragment
     */
    protected void rootViewNoNull() {

    }

    protected void beforeInitView() {

    }

    protected void afterInitView() {

    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        beforeInitView();
        if (rootView == null) {
            rootView = inflater.inflate(getLayoutRes(), container, false);
            initView(rootView);
        } else {
            rootViewNoNull();
        }

        ViewGroup parent = (ViewGroup) rootView.getParent();
        if (parent != null) {
            parent.removeView(rootView);
        }
        afterInitView();
        return rootView;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getActivity() instanceof OnCurrentFragmentHandler) {
            currentFragmentHandler = (OnCurrentFragmentHandler) getActivity();
        } else {
            //throw new ClassCastException("host activity must implement OnCurrentFragmentHandler");
        }


    }

    public Fragment getVisibleFragment() {
        FragmentManager fragmentManager = getFragmentManager();
        List<Fragment> fragments = fragmentManager.getFragments();
        for (Fragment fragment : fragments) {
            if (fragment != null && fragment.isVisible())
                return fragment;
        }
        return null;
    }


    @Override
    public void onStart() {
        super.onStart();
        if (currentFragmentHandler != null) {
            //Log.d(TAG, "handleCurrentFragment: " + getClass().getName());
            currentFragmentHandler.handleCurrentFragment(this);
        }
    }

    public boolean onKeyDown(int keyCode, KeyEvent event) {
        return false;
    }

    public boolean onKeyUp(int keyCode, KeyEvent event) {
        return false;
    }

    public boolean onKeyLongPress(int keyCode, KeyEvent event){
        return false;
    }

    public boolean onBackPressed() {
         if (getFragmentManager().getBackStackEntryCount() > 0) {
            //Log.d(TAG, "onBackPressed: popBackStack");
            getFragmentManager().popBackStack();
            return true;
        }
        return false;
    }

    public boolean onPlugOut() {
        return false;
    }

    public abstract void setCacheViewFromContent(View focusView);

    public abstract void setmCacheViewFromTab(View focusView);


    public interface OnCurrentFragmentHandler {
        void handleCurrentFragment(FragmentBase currentFragment);
    }
}
